<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input
              v-model="searchMap.where.goodsSpec.specName"
              class="input-width"
              placeholder="规格名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表 </span>
      <el-button
        type="success"
        size="mini"
        icon="el-icon-plus"
        @click="dialogFormVisible = true"
        >添加</el-button
      >
    </el-card>

    <!-- 添加规格 -->
    <el-dialog
      title="规格编辑"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
    >
      <el-form>
        <el-form-item label="规格名称" label-width="100px">
          <el-input v-model="temp.goodsSpec.specName"></el-input>
        </el-form-item>
        <el-form-item label="规格选项名称" label-width="100px">
          <el-input v-model="temp.optionName"></el-input>
        </el-form-item>

        <el-button type="success" icon="el-icon-plus" @click="addOptions()"
          >添加规格选项</el-button
        >
        <el-table :data="optionsA" style="width: 100%">
          <el-table-column prop="goodsSpec.specName" label="规格名称">
            <!-- <template slot-scope="scope">
              <el-input
                v-model="entity.options[scope.$index].goodsSpec.specName"
              ></el-input>
            </template> -->
          </el-table-column>
          <el-table-column prop="optionName" label="规格选项名称">
            <!-- <template slot-scope="scope">
              <el-input
                v-model="entity.options[scope.$index].optionName"
              ></el-input>
            </template> -->
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                @click="delOpt(scope.$index)"
                icon="el-icon-delete"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            optionsA = [];
            dialogFormVisible = false;
          "
          >取 消</el-button
        >
        <el-button type="primary" @click="saveSpec()">确 定</el-button>
      </div>
    </el-dialog>





    <el-table :data="entity.options" style="width: 100%">
      <el-table-column prop="goodsSpec.id" label="规格id">
        <!-- <template slot-scope="scope">
              <el-input
                v-model="entity.options[scope.$index].goodsSpec.specId"
              ></el-input>
            </template> -->
      </el-table-column>
      <el-table-column prop="goodsSpec.specName" label="规格名称">
        <!-- <template slot-scope="scope">
              <el-input
                v-model="entity.options[scope.$index].goodsSpec.specName"
              ></el-input>
            </template> -->
      </el-table-column>
      <el-table-column prop="optionName" label="规格选项名称">
        <!-- <template slot-scope="scope">
              <el-input
                v-model="entity.options[scope.$index].optionName"
              ></el-input>
            </template> -->
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="goodsupdate(scope.row)"
            icon="el-icon-delete"
          >
            修改
          </el-button>
          <el-button
            type="primary"
            @click="delO(scope.row.id)"
            icon="el-icon-delete"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      small
      :current-page="page"
      :page-size="pages"
      @current-change="findbypage"
      layout="prev, pager, next"
      :total="total"
    >
    </el-pagination>





    <el-dialog title="修改" :visible.sync="dialogVisible2" width="30%">
      <span slot="footer" class="dialog-footer">
        <!-- <el-form-item label="规格名称" label-width="100px"> -->
        <el-input
          v-model="objupdate.goodsSpec.specName"
          :disabled="true"
        ></el-input>
        <br />
        <br />
        <br />
        <!-- </el-form-item> -->
        <!-- <el-form-item label="规格选项名称" label-width="100px"> -->
        <el-input v-model="objupdate.optionName"></el-input>
        <br />
        <br />
        <br />
        <!-- </el-form-item> -->
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            dialogVisible2 = false;
            updateL();
          "
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>



<script>
import { findbypageG, addG, deleteG, updateG } from "../api/goods";
export default {
  data() {
    return {
      dialogVisible2: false,
      dialogFormVisible: false,
      optionsA: [],
      temp: {
        goodsSpec: {
          specName: "",
        },
      },
      entity: {
        options: [],
      },
      searchMap: {
        page: 1,
        size: 5,
        where: {
          name: "",
          goodsSpec:{
            specName:''
          }
        },
      },
      page:0,
      pages:0,
      total:0,
      objupdate: {
        goodsSpec: {
          specName: "",
        },
      },
    };
  },
  methods: {
    updateL() {
      updateG(this.objupdate)
        .then((e) => {
          this.$notify({
            title: "成功",
            message: "修改成功",
            type: "success",
          });
          this.objupdate = {
            goodsSpec: {
              specName: "",
            },
          };
          this.search();
        })
        .catch(() => {
          this.$notify({
            title: "失败",
            message: "修改失败",
            type: "success",
          });
        });
    },
    goodsupdate(obj) {
      this.dialogVisible2 = true;
      this.objupdate = obj;
    },
    delOpt(id) {
      this.optionsA.splice(id, 1);
    },
    delO(id) {
      deleteG(id)
        .then((e) => {
          this.searchMap.page=1;
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
          this.objupdate = {
            goodsSpec: {
              specName: "",
            },
          };
          this.search();
        })
        .catch(() => {
          this.$notify({
            title: "失败",
            message: "删除失败",
            type: "success",
          });
        });
    },
    addOptions() {
      this.optionsA.push(this.temp);
      this.temp = {
        goodsSpec: {
          specName: "",
        },
      };
    },
    saveSpec() {
      addG(this.optionsA)
        .then((e) => {
          this.dialogFormVisible = false;
          this.$notify({
            title: "成功",
            message: "添加成功",
            type: "success",
          });
          this.objupdate = {
            goodsSpec: {
              specName: "",
            },
          };
          this.search();
        })
        .catch(() => {
          this.$notify({
            title: "失败",
            message: "添加失败",
            type: "success",
          });
        });
    },
    search() {
      findbypageG(this.searchMap).then((e) => {
        console.log(this.searchMap);
        this.entity.options = e.list;
        this.page=e.pageNum;
        this.pages=e.pageSize;
        this.total=e.total;
      });
    },
    handleSearchList() {
      this.search();
    },
    findbypage(e) {
      this.searchMap.page = e;
      this.search();
    },
  },
  created() {
    this.search();
  },
};
</script>